import i18n from './i18n'

axios.defaults.withCredentials = true
// axios.defaults.headers.common['Accept-Language'] = lang
axios.defaults.baseURL = process.env.VUE_APP_BASE_API
// request拦截器
// axios.interceptors.request.use(
//   (config) => {
//     return config
//   },
//   (error) => {
//     console.log('request', error)
//     Promise.reject(error)
//   },
// )

// response 拦截器
axios.interceptors.response.use(
  (response) => {
    const res = response.data
    if (res.code && res.code !== 0) {
      return doError(response.config.url, res.code === 401, res.code, res.msg)
    }
    return response.data
  },
  (error) => {
    return doError(error && error.config && error.config.url, false, error && error.status, error && error.message)
  },
)


function doError(url, isLoginError, status, errMsg) {
  // console.log('1' + errMsg + '2')
  // console.log('i18n.t', i18n)
  // debugger
  errMsg = `${status || ''}|${i18n.t(errMsg) || errMsg || ''}`
  if (isLoginError) {
    // console.warn(errMsg)
  } else {
    // console.error(errMsg + '|' + url)
    Vue.toasted.show(
      errMsg,
      {
        theme: "bubble",
        position: "top-right",
        duration: 3000,
      }
    )
  }
  return Promise.reject(errMsg);
}

const get = axios.get;
const post = axios.post;
window.zz.get = get
window.zz.post = post
/**
 * 查询分页列表
 * @param modelName 例如  'order'    与Controller的@RequestMapper("/order")一致,默认为类名首字母小写
 * @param param     例如 { orderType:'购卡' }
 * @returns         例如 { total:2, records:[ {id:10000001,orderType:'购卡',detail:'购单人卡订单'}, {id:10000002,orderType:'购卡',detail:'购多人卡订单'} ] }
 */
window.zz.page = async function (modelName, params = {}) {
  return modelName && await get(`/${modelName}/page`, {params,
    paramsSerializer: params => {
      return Qs.stringify(params, { indices: false })
    }
  })
}
/**
 * 查询列表
 * @param modelName param 同上
 * @returns               例如 [ {id:10000001,orderType:'购卡',detail:'购单人卡订单'}, {id:10000002,orderType:'购卡',detail:'购多人卡订单'} ]
 */
window.zz.list = async function (modelName, param = {}) {
  return modelName && await get(`/${modelName}/list`, {params: param})
}
/**
 * 查询列表
 * @param modelName param 同上
 * @param id              例如 10000001
 * @returns               例如 {id:10000001,orderType:'购卡',detail:'购单人卡订单'}
 */
window.zz.one = async function (modelName, id = null, param = {}) {
  return modelName && await get(`/${modelName}/one`, {params: {...param, id}})
}

/**增改删*/
window.zz.add = async function (modelName, model) {
  return modelName && await post(`/${modelName}/add`, model)
}
window.zz.update = async function (modelName, model) {
  return modelName && await post(`/${modelName}/update`, model)  // 返回id
}
window.zz.delete = async function (modelName, id = null, model = {}) {
  id = (null != id && id) || model.id
  if (null == id) {
    let errMsg = '删除失败,ID为空';
    Vue.toasted.show(
      errMsg,
      {
        theme: "bubble",
        position: "top-right",
        duration: 3000,
      }
    )
    return Promise.reject(errMsg)
  }
  modelName && await post(`/${modelName}/deletes`, [id])
}
